# _*_ coding : UTF-8 _*_
# 开发人员  ： Administrator
# 开发时间  ：  9:55
# 文件名称  ： __init__.py.py
# 开发工具  ： PyCharm
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from redis import StrictRedis
from flask_wtf import CSRFProtect
from flask_session import Session
from config import config_dict
import logging
from logging.handlers import RotatingFileHandler

redis_store = None
db = SQLAlchemy()

# 定义工厂方法
def create_app(config_name):
    app = Flask(__name__)

    config = config_dict.get(config_name)

    log_file(config.LEVEL_NAME)

    app.config.from_object(config)

    db.init_app(app)


    # 创建redis对象
    global redis_store
    redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT, decode_responses=True)

    Session(app)

    CSRFProtect(app)

    from information.modules.index import index_blue
    app.register_blueprint(index_blue)

    return app

def log_file(level_name):
    logging.basicConfig(level=level_name)
    log_file_handler = RotatingFileHandler("logs/log", maxBytes=1024*1024*100, backupCount=10)
    formatter = logging.Formatter("%(levelname)s %(filename)s:%(lineno)d %(message)s")
    log_file_handler.setFormatter(formatter)
    logging.getLogger().addHandler(log_file_handler)






